﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using AdminUserLibrary.DataBaseModel;

namespace AdminUserLibrary
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class AdminUserService : IAdminUserService
    {
        #region Fields
        ServerIIDataBaseModelAdminContainer db = new ServerIIDataBaseModelAdminContainer();
        #endregion

        public bool AddUser(string email, string question, string answer)
        {
            bool ret = true;
            char[] charSeparators = new char[] {'@'};
            string[] name = email.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries); 
            if (String.IsNullOrWhiteSpace(email) || String.IsNullOrWhiteSpace(question) 
                || String.IsNullOrWhiteSpace(answer)) return false;
            try
            {
                User u = new User();
                u.Email = email;
                u.Question = question;
                u.Answer = answer;
                u.Name = name[0];
                db.Users.AddObject(u);
                db.SaveChanges();
            }
            catch { ret = false; }
            return (ret);
        }

        public bool AddUserParentField(string email, string parentfieldtitle, string category = "VARIOUS")
        {
            return (true);
        }

        public bool RemoveUser(string email)
        {
            bool ret = true;
            var res = (from parent in db.UserVsParentFeeds where parent.User.Email == email select parent.User).ToList();
            if (res.Count == 0) return false;
            try { db.Users.DeleteObject(res[0]); }
            catch { ret = false; }
            return (ret);
        }

        public bool RemoveUserParentField(string email, string parentfieldtitle, string category = "ALL")
        {
            throw new NotImplementedException();
        }

        public bool RemoveUserChildField(string email, string fieldtitle, string category = "ALL")
        {
            throw new NotImplementedException();
        }
    }
}
